package cn.swing.his.finance.webapi.service.impl;

import cn.swing.his.common.exception.HisServiceException;
import cn.swing.his.common.web.JsonPage;
import cn.swing.his.common.web.ResponseCode;
import cn.swing.his.finance.service.IFinanceFormService;
import cn.swing.his.finance.webapi.mapper.FinanceFormMapper;
import cn.swing.his.pojo.finance.dto.AddFinanceFormDTO;
import cn.swing.his.pojo.finance.entity.FinanceForm;
import cn.swing.his.pojo.finance.vo.FinanceFormVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class FinanceFormServiceImpl implements IFinanceFormService {
    @Autowired
    private FinanceFormMapper financeFormMapper;

//    获取所有财务表单
    @Override
    public JsonPage<FinanceFormVO> financeFormList(Integer pageNum, Integer pageSize,Integer index) {
        PageHelper.startPage(pageNum,pageSize);
        List<FinanceFormVO> list;
        if (index==0) {
            list = financeFormMapper.financeFormList();
        }else {
            list=financeFormMapper.financeFormListByState(index-1);
        }
        return JsonPage.restPage(new PageInfo<>(list));
    }
//修改申请表状态
    @Override
    public void updateFinanceFormState(Integer id) {
//        根据id查询当前状态
        Integer state=financeFormMapper.getFormState(id);
        if (state==1){
            throw new HisServiceException(ResponseCode.BAD_REQUEST,"已审批，请勿重复操作");
        }
        int rows=financeFormMapper.updateFinanceFormState(id,1);
        if (rows==0){//表示修改失败
            throw new HisServiceException(ResponseCode.BAD_REQUEST,"服务器异常");
        }
        LocalDateTime dealTime=LocalDateTime.now();
        financeFormMapper.updateFinanceDealTime(id,dealTime);
    }
    //    新增
    @Override
    public void insertFinanceForm(AddFinanceFormDTO addFinanceFormDTO) {
//        得到的是医生名字，先根据医生名字查找对于的id，并且根据id插入，如果未找到抛出异常
        log.debug("addFinanceFormDTO:{}",addFinanceFormDTO);
        Integer id=financeFormMapper.getDoctorIdByName(addFinanceFormDTO.getDoctorName());
        log.debug("id;{}"+id);
        System.out.println(id);
        if (id==null){
            throw new HisServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"该用户不存在，请重新检查！");
        }
        LocalDateTime now=LocalDateTime.now();
        FinanceForm financeForm= new FinanceForm();
        BeanUtils.copyProperties(addFinanceFormDTO,financeForm);
        System.out.println(financeForm);
        log.debug("financeForm:{}",financeForm);
        log.debug("doctorId:{}",financeForm.getDoctorId());
        financeForm.setDoctorId(id);
        financeForm.setState(0);
        financeForm.setDealTime(null);
        financeForm.setGmtCreated(now);
        int rows=financeFormMapper.insertFinanceForm(financeForm);
        if (rows==0){
            throw new HisServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"服务器异常，请稍后重试！");
        }
    }

    @Override
    public void updateBatchFinanceFormState(Integer id) {
        Integer state=financeFormMapper.getFormState(id);
        if (state==0){
            int rows=financeFormMapper.updateFinanceFormState(id,1);
            if (rows==1){//表示修改失败
                LocalDateTime dealTime=LocalDateTime.now();
                financeFormMapper.updateFinanceDealTime(id,dealTime);
            }
        }
    }

}
